package com.huatec.hiot_cloud.core.mqtt.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ Created by liwenqiang  on 2017/11/24 0024 at 上午 10:35  for mqtt
 * @ Description:
 */
@Service
public class SubCallBackService {
    @Autowired
    private ICoreServiceInner coreServiceInner;
    @Autowired
    private IMongoService mongoService;

    /** 掉线重连重新订阅*/
    public void initSub(){
        coreServiceInner.initSub();
    }

    /** TODO 设备消息处理*/
    public void msgHandle(String topic,String msg){
        System.out.println(topic);
        System.out.println(msg);
        if(topic.length()>10){
            boolean isValidTopic = coreServiceInner.isValidTopic(topic);
            if(isValidTopic){
                mongoService.saveToMongo(topic,msg);
            }
        }else if(msg.equals("offline")){
            coreServiceInner.updateOnlineStatus(topic,msg);
        }else if(msg.startsWith("response:")){
            coreServiceInner.returnResponse(topic,msg);
        }
    }
}
